package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDeviceInfoMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysRoadInfoMapper;
import com.ruoyi.system.domain.SysRoadInfo;
import com.ruoyi.system.service.ISysRoadInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 道路信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-14
 */
@Service
public class SysRoadInfoServiceImpl implements ISysRoadInfoService
{
    @Autowired
    private SysRoadInfoMapper sysRoadInfoMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysDeviceInfoMapper sysDeviceInfoMapper;
    /**
     * 查询道路信息
     *
     * @param id 道路信息主键
     * @return 道路信息
     */
    @Override
    public SysRoadInfo selectSysRoadInfoById(Integer id)
    {
        return sysRoadInfoMapper.selectSysRoadInfoById(id);
    }

    /**
     * 查询道路信息列表
     *
     * @param sysRoadInfo 道路信息
     * @return 道路信息
     */
    @Override
    public List<SysRoadInfo> selectSysRoadInfoList(SysRoadInfo sysRoadInfo)
    {
        return sysRoadInfoMapper.selectSysRoadInfoList(sysRoadInfo);
    }

    /**
     * 新增道路信息
     *
     * @param sysRoadInfo 道路信息
     * @return 结果
     */
    @Override
    public int insertSysRoadInfo(SysRoadInfo sysRoadInfo)
    {
        sysRoadInfo.setCreateTime(DateUtils.getNowDate());
        return sysRoadInfoMapper.insertSysRoadInfo(sysRoadInfo);
    }

    /**
     * 修改道路信息
     *
     * @param sysRoadInfo 道路信息
     * @return 结果
     */
    @Override
    public int updateSysRoadInfo(SysRoadInfo sysRoadInfo)
    {
        sysRoadInfo.setUpdateTime(DateUtils.getNowDate());
        return sysRoadInfoMapper.updateSysRoadInfo(sysRoadInfo);
    }

    /**
     * 批量删除道路信息
     *
     * @param ids 需要删除的道路信息主键
     * @return 结果
     */
    @Override
    public int deleteSysRoadInfoByIds(String ids)
    {
        return sysRoadInfoMapper.deleteSysRoadInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除道路信息信息
     *
     * @param id 道路信息主键
     * @return 结果
     */
    @Override
    public int deleteSysRoadInfoById(Integer id)
    {
        return sysRoadInfoMapper.deleteSysRoadInfoById(id);
    }

    /**
     * 定时导入字典
     */
    @Override
    public void importDict() {
        List<String> allSectionNameList = sysDeviceInfoMapper.getSectionNameList();
        if (Objects.equals(allSectionNameList, null) || Objects.equals(allSectionNameList.size(), 0)) {
            return;
        }
        SysDictData dictData = new SysDictData();
        dictData.setDictType("section_name");
        List<SysDictData> orgDictDataList = dictDataMapper.selectDictDataList(dictData);
        List<String> dictLabelList = new ArrayList<>();
        List<Long> dictSortList = new ArrayList<>();
        for (SysDictData data : orgDictDataList) {
            dictLabelList.add(data.getDictLabel());
            dictSortList.add(data.getDictSort());
        }
        List<String> notInsertDictSectionNameList = allSectionNameList.stream().filter(sectionName -> !dictLabelList.contains(sectionName)).collect(Collectors.toList());
        // 表里删去的,也在字典里删掉
        List<SysDictData> notDeleteDictSectionNameList = orgDictDataList.stream().filter(data -> !allSectionNameList.contains(data.getDictLabel())).collect(Collectors.toList());
        String[] notDeleteDictDictSectionNameIdStringList = new String[notDeleteDictSectionNameList.size()];
        for (int i = 0; i < notDeleteDictDictSectionNameIdStringList.length; i++) {
            notDeleteDictDictSectionNameIdStringList[i] = notDeleteDictSectionNameList.get(i).getDictCode().toString();
        }
        if (!Objects.equals(notDeleteDictDictSectionNameIdStringList, null) && !Objects.equals(notDeleteDictDictSectionNameIdStringList.length, 0)) {

            dictDataMapper.deleteDictDataByIds(notDeleteDictDictSectionNameIdStringList);
        }
        List<SysDictData> notInsertDictDataList = new ArrayList<>();
        for (String sectionName : notInsertDictSectionNameList) {
            SysDictData data = new SysDictData();
            data.setDictSort(1L);
            data.setDictLabel(sectionName);
            data.setDictValue(sectionName);
            data.setDictType("section_name");
            data.setIsDefault("Y");
            data.setStatus("0");
            data.setSearchValue("qrtz");
            data.setCreateTime(new Date());
            data.setUpdateTime(new Date());
            notInsertDictDataList.add(data);
        }
        for (SysDictData data : notInsertDictDataList) {
            dictDataMapper.insertDictData(data);
        }
    }
}
